package com.gitee.feizns.explore.data_structure.string;

/**
 * 125. 验证回文串
 * @author feizns
 * @since 2020/1/16
 */
public class IsPalindrome {

    public static void main(String[] args) {
        System.out.println(isPalindrome(".,"));
    }

    public static boolean isPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while ( i < j ) {
            while ( i < j && !isLetterOrNumber(s.charAt(i)) ) {
                i++;
            }
            while ( i < j && !isLetterOrNumber(s.charAt(j)) ) {
                j--;
            }
            if ( i < j ) {
                if ( Character.toUpperCase(s.charAt(i)) == Character.toUpperCase(s.charAt(j)) ) {
                    i++;
                    j--;
                } else
                    return false;
            }
        }
        return true;
    }

    private static boolean isLetterOrNumber(char s) {
        return Character.isLetter(s) || Character.isDigit(s);
    }

}
